package other.edan.newpaas.agent;

import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEvent;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventRequest;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventResponse;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import other.edan.newpaas.utils.HCXObjectService;

/**
 * @Description: 自动流: 处理'签约代理申请'下的 '可售产品'、'采购计划'
 * @Author: 黎炎松
 * @Date: 2025/1/8 15:05
 */
public class DealAgentSigning implements AutoFlowEvent {
    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "签约代理申请自动流 处理代理申请下的产品、计划 ==> ";

    @Override
    public AutoFlowEventResponse execute(AutoFlowEventRequest request) throws ScriptBusinessException {
        LOGGER.debug(LOGGER_MSG + " 触发开始 ");
        LOGGER.info(request.toString());
        Long dataId = request.getDataId();

        // 获取 签约代理申请 下最新的代理协议
        String sql = String.format("select id from agentAgreement__c where agentSigning__c = %s order by createdAt desc", dataId);
        XObject xObject = HCXObjectService.queryOne(sql);
        if (xObject != null) {
            LOGGER.debug(LOGGER_MSG + "开始处理代理申请下的产品、计划");
            Long id = xObject.getId();
            DealAgentApply dealAgentApply = new DealAgentApply();
            dealAgentApply.dealAgentApplyDetail(id);
        }

        AutoFlowEventResponse response = new AutoFlowEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        return response;
    }
}
